﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuanLyTruongHoc_App
{
    public partial class QLDanhGia : Form
    {

        public QLDanhGia()
        {
            InitializeComponent();
        }

        private void QLHinhThucCanBo_Load(object sender, EventArgs e)
        {
            refreshData();
        }

        private void refreshData()
        {
            tbl_DanhGiaTableAdapter.Connection.ConnectionString = PublicLib.getStrConn();
            this.tbl_DanhGiaTableAdapter.FillByFlag(this.qLTH.tbl_DanhGia, PConst.FLAG_NONE);
        }

        private void bt_ThemMoi_Click(object sender, EventArgs e)
        {
            dg_DanhSach.Rows[dg_DanhSach.Rows.Count - 1].Cells[tenDataGridViewTextBoxColumn.Name].Selected = true;
            dg_DanhSach.BeginEdit(true);
        }

        private void dg_DanhSach_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (dg_DanhSach.Rows[e.RowIndex].Cells[idDataGridViewTextBoxColumn.Name].Value == null)
                return;

            if (dg_DanhSach.Rows[e.RowIndex].Cells[idDataGridViewTextBoxColumn.Name].Value.ToString() == "") // add new
            {
                int newId = PublicLib.getMaxIdFromTbl(qLTH.tbl_DanhGia.TableName, qLTH.tbl_DanhGia.idColumn.ColumnName) + 1;
                dg_DanhSach.Rows[e.RowIndex].Cells[idDataGridViewTextBoxColumn.Name].Value = newId;
                dg_DanhSach.Rows[e.RowIndex].Cells[flagDataGridViewTextBoxColumn.Name].Value = PConst.FLAG_NONE;
            }
        }

        private void fillSTT()
        {
            for (int i = 0; i < dg_DanhSach.RowCount; i++)
            {
                dg_DanhSach.Rows[i].Cells[cl_STT.Name].Value = (i + 1).ToString();
            }
        }

        private void dg_DanhSach_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            fillSTT();
        }

        private void bt_Sua_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count == 0)
                return;
            dg_DanhSach.SelectedRows[0].Cells[tenDataGridViewTextBoxColumn.Name].Selected = true;
            dg_DanhSach.BeginEdit(true);
        }

        private void dg_DanhSach_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            deleteByID(e.Row.Cells[idDataGridViewTextBoxColumn.Name].Value.ToString());
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count <= 0)
                return;
            deleteByID(dg_DanhSach.SelectedRows[0].Cells[idDataGridViewTextBoxColumn.Name].Value.ToString());
        }

        private void deleteByID(String _id)
        {
            try
            {
                PublicLib.updateField(qLTH.tbl_DanhGia.TableName,
                                        qLTH.tbl_DanhGia.idColumn.ColumnName,
                                        _id,
                                        qLTH.tbl_DanhGia.flagColumn.ColumnName,
                                        PConst.FLAG_DELETE.ToString());
                refreshData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(MSG.MSG_ERROR_INSERT_DB + " " + ex.Message);
            }
        }

        private void tblDanhGiaBindingSource_CurrentItemChanged(object sender, EventArgs e)
        {
            if ((DataRowView)((BindingSource)sender).Current == null)
                return;
            DataRow ThisDataRow = ((DataRowView)((BindingSource)sender).Current).Row;
            if (ThisDataRow.RowState == DataRowState.Modified ||
                ThisDataRow.RowState == DataRowState.Added)
            {
                tbl_DanhGiaTableAdapter.Update(ThisDataRow);
            }
        }
    }
}
